<?php

/**
 * Subclass for performing query and update operations on the 'sms_output' table.
 *
 * 
 *
 * @package lib.model
 */ 
class SmsOutputPeer extends BaseSmsOutputPeer
{
    public static function getSms($phoneNumber, $createdDatetimeFrom, $createdDatetimeTo, $limit, $offset){        
        $con = new MysqlDAL('propel');
        $sql ="SELECT sms_output.id,
                       sms_output.receive_phone as phoneNumber,
                       sms_id as smsId,
                       sms_output.service_number as serviceNumber,
                       sms_output.send_datetime as createdDatetime,
                       sms_output.service_name as serviceName,
                       sms_output.content,
                       sms_output.status
                from sms_output";
        if($phoneNumber != "" || $createdDatetimeFrom != "" || $createdDatetimeTo != ""){
            $sql.= " where sms_output.id > 0";
        }
        if($phoneNumber != ""){
            $sql.= " AND receive_phone = ".$phoneNumber;
        }
        if($createdDatetimeFrom != ""){
            $sql.= " AND UNIX_TIMESTAMP(send_datetime) > ".$createdDatetimeFrom;
        }
        if($createdDatetimeTo != ""){
            $sql.= " AND UNIX_TIMESTAMP(send_datetime) < ".$createdDatetimeTo;
        }
        $sql.= " ORDER BY sms_output.send_datetime DESC ";
        if($limit){
            $sql.=" LIMIT ".$limit;
        }
        if($offset){
            $sql.=" OFFSET ".$offset;
        }
        return $con->doSelect($sql);
    }
    public static function getTotalSms($phoneNumber, $createdDatetimeFrom, $createdDatetimeTo){
        $con = new MysqlDAL('propel');
        $sql= "select count(*) as totalSms from sms_output";
        if($phoneNumber != "" || $createdDatetimeFrom != "" || $createdDatetimeTo != ""){
            $sql.= " where sms_output.id > 0";
        }
        if($phoneNumber != ""){
            $sql.= " AND receive_phone = ".$phoneNumber;
        }
        if($createdDatetimeFrom != ""){
            $sql.= " AND UNIX_TIMESTAMP(send_datetime) > ".$createdDatetimeFrom;
        }
        if($createdDatetimeTo != ""){
            $sql.= " AND UNIX_TIMESTAMP(send_datetime) < ".$createdDatetimeTo;
        }
        $totalSms = $con->doSelect($sql);
        if($totalSms){
            $totalSms = $totalSms[0]['totalSms'];
        }else{
            $totalSms = 0;
        }
        return $totalSms;
    }
}
